<?php

class ForeseeDiagrams {
    public static function get_saved($id) {
        $id = intval($id);
        $sql_result = DB::query("SELECT name, value FROM foresee_hystory WHERE id = $id");
        return DB::row($sql_result);
    }
    
    public static function draw_request($request, $subdivisions) {
        /* Cecking structure */
        if (!array_key_exists('new_measure', $request) || !array_key_exists('subdivision', $request) || !array_key_exists('administrated', $request)) {
            return;
        }
        
        foreach ($request['new_measure'] as $key=>$name) {
            $name = htmlspecialchars($name);
            echo "<h3>Paramos priemonė <i>$name</i></h3>";
            if (array_key_exists($key, $request['subdivision'])) {
                echo "<div style='width: 50%; display: inline-table;'><b>Administravimo laikas</b><br/>";
                foreach ($request['subdivision'][$key] as $subdivision=>$time) {
                    $subdivision = htmlspecialchars($subdivisions[$subdivision]);
                    $time = floatval($time);
                    echo "<i>$subdivision</i>: $time<br/>";
                }
                echo "</div>";
            }
            if (array_key_exists($key, $request['administrated'])) {
                echo "<div style='width: 50%; display: inline-table;'><b>Paraiškų kiekiai</b><br/>";
                foreach ($request['administrated'][$key] as $date=>$count) {
                    $date = htmlspecialchars($date);
                    $count = intval($count);
                    echo "<i>$date</i>: $count<br/>";
                }
                echo "</div>";
            }
        }
    }
    
    public static function draw($request, $subdivisions, $dates, $items_in_width = null, $width = 1000) {
        /* Original sudivisions data */
        $measures = get_support_measures_associative();
        $dataset = SubdivisionData::counts_to_dataset();

        $measures_subdivisions = SubdivisionData::get_measures_subdivisions();
        $time_groups = SubdivisionData::dataset_to_table($dataset, $subdivisions, $dates, $is_time_groups, $measures_subdivisions);
        SubdivisionData::predict($time_groups);
        
        /* Original information systems data */
        require 'Processing/InformationSystems/is_data_processing.php';
        $is_names = get_is_associative();
        ISData::predict_is($is_time_groups);

        /* Input */
        if ($request) {
            $new_measures = $request['new_measure'];
            $measures2 = $measures + $new_measures;

            $date_keys = array_keys($dates);
            $index = count($dataset);
            $predicted_dates = 12;
            foreach ($new_measures as $measure=>$name) {
                /* Empty dates */
                foreach($date_keys as $date) {
                    if (!$dates[$date]) {
                        $new_dataset[$index] = array($date, $measure, 0, 0);
                        $index++;
                    }
                }

                /* Manually predicted dates */
                foreach ($request['administrated'][$measure] as $date=>$administrated) {
                    $administrated = $request['administrated'][$measure][$date];
                    if (is_numeric($administrated)) {
                        //TODO: clean up only administrated
                        $new_dataset[$index] = array($date, $measure, $administrated, $administrated);
                    } else {
                        $new_dataset[$index] = array($date, $measure, 0, 0);
                    }
                    $index++;
                }
            }
            $dataset2 = $dataset + $new_dataset;

            $random = 0;
            foreach ($subdivisions as $subdivision=>$value) {
                foreach ($new_measures as $measure=>$name) {
                    if ($request['subdivision'][$measure][$subdivision] && is_numeric($request['subdivision'][$measure][$subdivision])) {
                        $new_measures_subdivisions[$measure][$subdivision] = $request['subdivision'][$measure][$subdivision];
                    } else {
                        $new_measures_subdivisions[$measure][$subdivision] = 0;
                    }
                }
                $random++;
            }
            $measures_subdivisions2 = $measures_subdivisions + $new_measures_subdivisions;

            $time_groups2 = SubdivisionData::dataset_to_table($dataset2, $subdivisions, $dates, $is_time_groups2, $measures_subdivisions2);
            SubdivisionData::predict($time_groups2);
            ISData::predict_is($is_time_groups2);
        }

        /* Graphics and tables */
        if ($request) {
            /* Caching */
            $cache_date = get_load_last_modified();

            /* For spliting diagrams */
            $margin = 40 * 2;
            $width -= $margin;
            $divided_dates = SubdivisionData::divide_dates($dates, $items_in_width, $time_groups, $new_time_groups);
            SubdivisionData::divide_dates($dates, $items_in_width, $time_groups2, $new_time_groups2);
            SubdivisionData::divide_dates($dates, $items_in_width, $is_time_groups, $new_is_time_groups);
            SubdivisionData::divide_dates($dates, $items_in_width, $is_time_groups2, $new_is_time_groups2);
            $n = count($divided_dates);
            if ($items_in_width) {
                $step_size = $width / $items_in_width;
                $table_width = $width;
            } else {
                $step_size = $width / count($dates);
                $table_width = null;
            }
            
            /* Default showing */
            //TODO: send showed elements as well
            $default_show = true;
            if ($items_in_width) {
                $default_hide = true;
            } else {
                $default_hide = false;
            }
            
            
            require 'GUI/Tabs/LoadView/LoadViewDesign.php';
            MainDesign::viewSectionBegin('Dabartinė padalinių diagrama', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    if ($items_in_width === null) {
                        $file = "sub_%MODE%t.gif";
                    } else {
                        $file = "sub_%MODE%t_sl_$i.gif";
                    }
                    LoadViewDesign::php_linear_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $cache_date, $current_width, 300, $file, $last);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin("Numatoma padalinių diagrama", $default_show);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    if ($items_in_width === null) {
                        $file = "fs_sub_%MODE%t_.gif";
                    } else {
                        $file = "fs_sub_%MODE%t_sl_$i.gif";
                    }
                    LoadViewDesign::php_linear_sub($new_dates, $subdivisions, $new_time_groups2[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, false, $current_width, 300, $file, $last);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Dabartinė IS diagrama', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    if ($items_in_width === null) {
                        $file = "is_%MODE%t.gif";
                    } else {
                        $file = "is_%MODE%t_sl_$i.gif";
                    }
                    LoadViewDesign::php_linear_is($new_dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $cache_date, $current_width, 300, $file, $last);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin("Numatoma IS diagrama", $default_show);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    if ($items_in_width === null) {
                        $file = "fs_is_%MODE%t_.gif";
                    } else {
                        $file = "fs_is_%MODE%t_sl_$i.gif";
                    }
                    LoadViewDesign::php_linear_is($new_dates, $is_names, $new_is_time_groups2[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, false, $current_width, 300, $file, $last);
                }
            MainDesign::viewSectionEnd();            
            
            MainDesign::viewSectionBegin('Dabartinė padalinių kiekių lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Numatoma padalinių kiekių lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_sub($new_dates, $subdivisions, $new_time_groups2[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Dabartinė padalinių laiko lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Numatoma padalinių laiko lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_sub($new_dates, $subdivisions, $new_time_groups2[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Dabartinė IS kiekių lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_is($new_dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Numatoma IS kiekių lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_is($new_dates, $is_names, $new_is_time_groups2[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Dabartinė IS laiko lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_is($new_dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $table_width);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Numatoma IS laiko lentelė', $default_hide);
                foreach ($divided_dates as $i=>$new_dates) {
                    $current_width = floor($step_size * count($new_dates)) + $margin;
                    $last = ($i == $n - 1);
                    LoadViewDesign::table_is($new_dates, $is_names, $new_is_time_groups2[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $table_width);
                }
            MainDesign::viewSectionEnd();

            /* Showing only predicted elements */
            $predicted_start = count($dates) - 12;
            $dates = array_slice($dates, $predicted_start);
            foreach ($time_groups2 as $subdivision=>$row) {
                $time_groups3[$subdivision] = array_slice($time_groups[$subdivision], $predicted_start);
                $time_groups4[$subdivision] = array_slice($row, $predicted_start);
            }
            foreach ($is_time_groups2 as $is=>$row) {
                $is_time_groups3[$is] = array_slice($is_time_groups[$is], $predicted_start);
                $is_time_groups4[$is] = array_slice($row, $predicted_start);
            }
            if ($items_in_width) {
                $divided_dates = array();
                $divided_dates[] = array_slice($dates, 0, 6);
                $divided_dates[] = array_slice($dates, 6);
            } else {
                $divided_dates = array($dates);
            }

            MainDesign::viewSectionBegin('Palyginamoji padalinių lentelė', $default_hide);
                foreach($divided_dates as $i=>$new_dates) {
                    LoadViewDesign::table_compare_sub($new_dates, $subdivisions, $time_groups3, $time_groups4);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Palyginamoji padalinių kiekių diagrama', $default_show);
                LoadViewDesign::linear_compare2($dates, $subdivisions, $time_groups3, $time_groups4, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Palyginamoji padalinių laiko diagrama', $default_hide);
                LoadViewDesign::linear_compare2($dates, $subdivisions, $time_groups3, $time_groups4, LoadViewDesign::DIAGRAM_MODE_TIME);
            MainDesign::viewSectionEnd();
    //
            MainDesign::viewSectionBegin('Palyginamoji informacinių sistemų lentelė', $default_hide);
                foreach($divided_dates as $i=>$new_dates) {
                    LoadViewDesign::table_compare_is($new_dates, $is_names, $is_time_groups3, $is_time_groups4);
                }
            MainDesign::viewSectionEnd();

            MainDesign::viewSectionBegin('Palyginamoji IS kiekių diagrama', $default_show);
                LoadViewDesign::linear_compare2($dates, $is_names, $is_time_groups3, $is_time_groups4, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
            MainDesign::viewSectionEnd();
    //
            MainDesign::viewSectionBegin('Palyginamoji IS laiko diagrama', $default_hide);
                LoadViewDesign::linear_compare2($dates, $is_names, $is_time_groups3, $is_time_groups4, LoadViewDesign::DIAGRAM_MODE_TIME);
            MainDesign::viewSectionEnd();
        }
    }
}